<?php

class Reservations_HallsSuccessView extends SrsReservationsBaseView
{
	public function executeHtml(AgaviRequestDataHolder $rd)
	{
		$this->setupHtml($rd);

		$halls = $this->fetchHalls($rd);

		$this->setAttribute('noHalls', empty($halls));
		$this->setAttribute('halls', $halls);
	}

	private function fetchHalls(AgaviRequestDataHolder $rd)
	{
		$q = Doctrine_Query::create()->from('Hall')->orderBy('address ASC, name ASC');

		$dir   = AgaviConfig::get('core.uploads_dir') . '/halls/';
    $halls = array();

		$capacity = 0;
    
    foreach ($q->fetchArray() as $row)
    {
      $hall = array(
        'id'       => $row['id'],
        'name'     => e($row['name']),
        'address'  => e($row['address']),
        'capacity' => $row['capacity'],
        'thumb'    => '',
      );

			$images = Hall::getImages($dir . $hall['id']);

      $hall['thumb'] = empty($images) ? '/img/no-image.png' : '/uploads/halls/' . $hall['id'] . '/thumb_' . $images[array_rand($images)];

      $halls[] = $hall;

			if ($row['capacity'] > $capacity)
			{
				$capacity = $row['capacity'];
			}
    }

		$this->setAttribute('maxCapacity', $capacity);

    return $halls;
	}
}